package com.timi_licai.timi_licai_account.database;


import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 数据库操作的工具类
 */
public class DatabaseUtils {

    private static DBHelper helper;

    public DatabaseUtils(DBHelper helper) {
        this.helper = helper;
    }

    /**
     * 查询数据库
     *
     * @param tableName     表名称
     * @param projection    查询的列
     * @param selection     查询条件
     * @param selectionArgs 查询条件的值
     * @param sortOrder     排序
     * @return
     */
    public static Cursor query(String tableName,
                               String[] projection,
                               String selection,
                               String[] selectionArgs,
                               String sortOrder) {
        Cursor ret = null;
        SQLiteDatabase database = helper.getWritableDatabase();
        if (database != null) {
            ret = database.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);
        }
        return ret;
    }

    /**
     * 插入数据库
     *
     * @param tableName 表名称
     * @param values    插入的键值对
     * @return
     */
    public static boolean insert(String tableName, ContentValues values) {
        boolean isSuccessful = false;
        SQLiteDatabase database = helper.getWritableDatabase();

        database.beginTransaction();
        if (database != null) {
            try {
                long rid = database.insert(tableName, null, values);
                if (rid > 0) {
                    isSuccessful = true;
                    database.setTransactionSuccessful();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                database.endTransaction();
            }
        }
        return isSuccessful;
    }

    /**
     * 修改数据库的表
     *
     * @param tableName     标的名称
     * @param values        要修改的键值对
     * @param selection     修改的条件
     * @param selectionArgs 修改条件的值
     * @return 返回的修改的条目的id
     */
    public static int update(String tableName, ContentValues values, String selection, String[] selectionArgs) {
        int ret = 0;
        SQLiteDatabase database = helper.getWritableDatabase();
        if (database != null) {
            database.beginTransaction();
            try {
                ret = database.update(tableName, values, selection, selectionArgs);
                Log.d("**", "ret=" + ret);
                database.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                database.endTransaction();
            }
        }

        return ret;
    }

    /**
     * 删除数据库表中的内容
     *
     * @param tableName     表名称
     * @param selection     删除的条件
     * @param selectionArgs 删除条件的值
     * @return 删除条目的总数
     */

    public static int delete(String tableName, String selection, String[] selectionArgs) {
        int ret = 0;
        SQLiteDatabase database = helper.getWritableDatabase();
        if (database != null) {
            database.beginTransaction();
            try {
                ret = database.delete(tableName, selection, selectionArgs);
                database.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                database.endTransaction();
            }

        }
        return ret;
    }
}
